#    This file is a part of the WBTS Project. WBTS is a system for helping 
#	 researchers, and browser implementers to find security related bugs.
#    Copyright (C) 2010 Isaac Dawson
#
#    WBTS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Note:
# This file must be run with twistd (unix) or twistd.py (windows)!!!
# Example: twistd -noy wbts.tac

import glob
import sys
import os
sys.path.append(os.curdir) # allows twistd to find the packages
                           # so we can just run from any dir
                           # without requiring a setup.py script

from twisted.application import service
from twisted.web import script, static, twcgi

from wbts import wbtsservice
from wbts.config.wbts_config import WBTSConfig

def chown_files(path, uid, gid):
    path = os.path.dirname(path)
    for root, dirs, files in os.walk(path):
        for subdirs in dirs:
            os.chown(os.path.join(root, subdirs), uid, gid)
        for subfiles in files:
            os.chown(os.path.join(root, subfiles), uid, gid)

def validate_perms(options, uid, gid):
    dns_directory = options.getCfgValue('DNSSettings','named_root')
    dbtype = options.getCfgValue('DBClientSettings', 'db_class')
    
    try:
        os.chown(os.path.dirname(dns_directory), uid, gid) # directory itself
        chown_files(dns_directory, uid, gid)
        print "[STARTUP] Chowning our DNS directory."
        if (dbtype != 'mongodb.WBTSMongoDB'):
            data_dir = options.getCfgValue('DBClientSettings',
                                           'db_connectstring')
            print "[STARTUP] Chowning our data directory."
            os.chown(os.path.dirname(data_dir), uid, gid) # the directory itself
            chown_files(data_dir, uid, gid)
            
    except IOError, msg:
        print "ERROR we are unable to chown %s. Which means we "\
        "can not update DNS.\n Exiting..."
        exit(1)
    return


application = None
options = WBTSConfig()
options.loadCfg()

wbts_serv = wbtsservice.makeService(options)
if (os.name == 'nt'):
    application = service.Application('wbts')
else:
    uid = options.getCfgValue('PrivilegeSettings','userid')
    gid = options.getCfgValue('PrivilegeSettings','groupid')
    validate_perms(options, uid, gid)
    application = service.Application('wbts', uid, gid)
wbts_serv.setServiceParent(service.IServiceCollection(application))
